import React, { useEffect, useRef, useState } from 'react';
import {
    Button, Input, Form, message, InputNumber
} from 'antd';
import Checkbox from 'antd/lib/checkbox/Checkbox';
import request from '@/utils/request';
import { FormattedMessage, useIntl } from 'umi';

const apiURL = "/api/services/app/WhseBin/";

const WhseBinEntry: React.FC<any> = (props) => {
    const [loading, setLoading] = useState(false);
    const formRef = useRef<any>(null);
    const intl = useIntl();

    useEffect(() => {
        fetchDatas();
    }, []);

    const onFinish = (values: any) => {
        var model = values.model;
        model.id = props.id;
        setLoading(true);
        var act = "Modify";
        if (model.id == 0 || !model.id) {
            act = "New";
        }
        model.zoneID = "0";
        model.binType = "0";
        request(apiURL + act, {
            method: 'POST',
            data: model,
        }).then((response) => {
            if (response) {
                if (response.success) {
                    if (response.result.success) {
                        message.success(intl.formatMessage({ id: 'pages.saveSuccess' }));
                        props.fetchData();
                    }
                    else {
                        message.error(response.result.message);
                    }
                }
                setLoading(false);
            }
        });
    }

    const fetchDatas = () => {
        const { id, WareHse } = props;
        var model: any = {}
        model.warehouseCode = WareHse?.warehouseCode;
        if (id > 0) {
            request(apiURL + 'Get', {
                method: 'GET',
                params: { Id: id },
            }).then((response) => {
                model = response?.result;
                formRef.current.setFieldsValue({ model });
            });
        }
        else
            formRef.current.setFieldsValue({ model });
    }

    const layout = {
        labelCol: { span: 5 },
        wrapperCol: { span: 19 },
    };

    return (
        <Form {...layout} ref={formRef} onFinish={onFinish} >
            <Form.Item label={<FormattedMessage id="pages.warehouseCode" />} style={{ marginBottom: 0 }}>
                <Input.Group compact>
                    <Form.Item name={['model', 'warehouseCode']} rules={[{ required: true }]}>
                        <Input disabled />
                    </Form.Item>
                    <Form.Item>
                        <Input disabled value={props.WareHse?.description} />
                    </Form.Item>
                </Input.Group>
            </Form.Item>
            <Form.Item name={['model', 'binNum']} label={<FormattedMessage id="pages.binNum" />} rules={[{ required: true, message: intl.formatMessage({ id: 'pages.binNum.required' }) }]}>
                <Input />
            </Form.Item>
            <Form.Item name={['model', 'description']} label={<FormattedMessage id="pages.binDesc" />} rules={[{ required: true, message: intl.formatMessage({ id: 'pages.binDesc.required' })  }]}>
                <Input />
            </Form.Item>
            <Form.Item name={['model', 'maxFill']} label={<FormattedMessage id="pages.stock(max)" />}>
                <InputNumber min={0} />
            </Form.Item>
            <Form.Item wrapperCol={{ offset: 5 }}>
                <Input.Group compact>
                    <Form.Item name={['model', 'nonNettable']} valuePropName="checked">
                        <Checkbox><FormattedMessage id="pages.nonNettable" /></Checkbox>
                    </Form.Item>
                    <Form.Item name={['model', 'inActive']} valuePropName="checked">
                        <Checkbox><FormattedMessage id="pages.invalid" /></Checkbox>
                    </Form.Item>
                    <Form.Item name={['model', 'inAdjustable']} valuePropName="checked">
                        <Checkbox><FormattedMessage id="pages.inAdjustable" /></Checkbox>
                    </Form.Item>
                </Input.Group></Form.Item>
            <Form.Item wrapperCol={{ offset: 5 }}>
                <Button type="primary" htmlType="submit"><FormattedMessage id="pages.save" /></Button>
            </Form.Item>
        </Form>
    )
}

export default WhseBinEntry;